/**
 * 
 */
package edu.umd.clip.lm.util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public final class IO {
	public static final String GZIP_SUFFIX = ".gz";
	public static final String CHARSET = "UTF-8";
	public static final Charset charset = Charset.forName(CHARSET);

	public static InputStream getInputStream(String fname) throws IOException {
		InputStream in = new FileInputStream(fname); 
		if (fname.endsWith(GZIP_SUFFIX)) {
			in = new GZIPInputStream(in);
		}
		return in;
	}
	
	public static InputStream getInputStream(File file) throws IOException {
		return getInputStream(file.getPath());
	}
	
	public static OutputStream getOutputStream(String fname) throws IOException {
		OutputStream out = new FileOutputStream(fname);
		if (fname.endsWith(GZIP_SUFFIX)) {
			out = new GZIPOutputStream(out);
		}
		return out;
	}
	
	public static OutputStream getOutputStream(File file) throws IOException {
		return getOutputStream(file.getPath());
	}

	public static BufferedReader getReader(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
		return reader;
	}
	
	public static BufferedWriter getWriter(OutputStream os) {
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, charset));
		return writer;
	}
	
}
